package com.sync.data.base;

import cn.hutool.core.util.StrUtil;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.sync.data.common.annotation.FieldProps;
import com.sync.data.common.annotation.FieldType;
import com.sync.data.common.annotation.RenderType;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.Hibernate;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.List;


@MappedSuperclass
@Getter
@Setter
public class BaseEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Override
    public boolean equals(Object o){

        BaseEntity b = (BaseEntity) o;

        if(this == b){
            return true;
        }

        if(b == null){
            return false;
        }

        if(StrUtil.isEmpty(b.getId())){
            return false;
        }

        if(StrUtil.isEmpty(this.getId())){
            return false;
        }

        return this.getId().equals(b.getId());
    }

    /**
     * 主键
     */
    @Id
    @GeneratedValue(generator = "commonIdGenerator")
    @GenericGenerator(name = "commonIdGenerator", strategy = "com.sync.data.common.interceptor.CommonIdGenerator")
    @FieldProps(type = FieldType.STRING, label = "id")
    public String id;

    @FieldProps(type = FieldType.STRING, label = "名称", renderType = RenderType.Input)
    public String name;

    /**
     * 状态
     */
    @Column(name = "status")
    @FieldProps(type = FieldType.DICT, label = "状态", renderType = RenderType.Select)
    private String statusField;
    /**
     * 类型
     */
    @Column(name = "record_type")
    @FieldProps(type = FieldType.DICT, label = "类型", renderType = RenderType.Select, readOnly = true)
    private String recordTypeField;
    /**
     * 创建人id
     */
    @CreatedBy
    @Column(updatable = false)
    private String createBy;


    /**
     * 创建日期
     */
    @CreatedDate
    @Column(updatable = false)
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;
    /**
     * 更新人
     */
    @LastModifiedBy
    private String updateBy;

    /**
     * 更新日期
     */
    @LastModifiedDate
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;


    @Transient
    private boolean favor;

    @Transient
    private boolean force = false;

    @Transient
    private boolean added = false;

}
